package cn.iocoder.yudao.module.reading.controller.app.recordings;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.reading.controller.app.recordings.vo.*;
import cn.iocoder.yudao.module.reading.service.recordings.RecordingsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * 用户 APP - 配音管理
 *
 * @author 李晨
 */
@Tag(name = "用户 APP - 配音管理")
@RestController
@RequestMapping("/reading/user-recordings")
@Validated
@Slf4j
public class RecordingsController {

    @Resource
    private RecordingsService recordingsService;

    @GetMapping("")
    @Operation(summary = "获取用户配音列表")
    @PreAuthenticated
    public CommonResult<PageResult<UserRecordingRespVO>> getUserRecordingList(
            @Parameter(description = "页码", example = "1") @RequestParam(value = "page", defaultValue = "1") Integer page,
            @Parameter(description = "每页数量", example = "10") @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        Long userId = getLoginUserId();
        log.info("获取用户配音列表，用户ID: {}, 页码: {}, 每页数量: {}", userId, page, limit);

        PageParam pageParam = new PageParam();
        pageParam.setPageNo(page);
        pageParam.setPageSize(limit);

        PageResult<UserRecordingRespVO> result = recordingsService.getUserRecordingList(userId, pageParam);
        return success(result);
    }

    @GetMapping("/aggregate")
    @Operation(summary = "获取用户配音聚合列表（按绘本聚合）")
    @PreAuthenticated
    public CommonResult<PageResult<UserRecordingAggregateRespVO>> getUserRecordingAggregateList(
            @Parameter(description = "页码", example = "1") @RequestParam(value = "page", defaultValue = "1") Integer page,
            @Parameter(description = "每页数量", example = "10") @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        Long userId = getLoginUserId();
        log.info("获取用户配音聚合列表，用户ID: {}, 页码: {}, 每页数量: {}", userId, page, limit);

        PageParam pageParam = new PageParam();
        pageParam.setPageNo(page);
        pageParam.setPageSize(limit);

        PageResult<UserRecordingAggregateRespVO> result = recordingsService.getUserRecordingAggregateList(userId, pageParam);
        return success(result);
    }

    @GetMapping("/detail/{bookId}")
    @Operation(summary = "获取用户配音详情页信息")
    @PreAuthenticated
    public CommonResult<UserRecordingDetailPageRespVO> getUserRecordingDetailPage(
            @Parameter(description = "绘本ID", required = true, example = "1") @PathVariable("bookId") Long bookId) {
        Long userId = getLoginUserId();
        log.info("获取用户配音详情页信息，用户ID: {}, 绘本ID: {}", userId, bookId);

        UserRecordingDetailPageRespVO result = recordingsService.getUserRecordingDetailPage(userId, bookId);
        return success(result);
    }

    @PutMapping("/public-setting")
    @Operation(summary = "批量设置绘本配音的公开状态")
    @PreAuthenticated
    public CommonResult<Boolean> setBookRecordingsPublic(@RequestBody @Valid UserRecordingPublicSettingReqVO reqVO) {
        Long userId = getLoginUserId();
        log.info("批量设置绘本配音公开状态，用户ID: {}, 绘本ID: {}, 公开状态: {}", userId, reqVO.getBookId(), reqVO.getIsPublic());

        recordingsService.setBookRecordingsPublic(userId, reqVO.getBookId(), reqVO.getIsPublic());
        return success(true);
    }

    @PostMapping("/share/{bookId}")
    @Operation(summary = "生成绘本配音分享信息")
    @PreAuthenticated
    public CommonResult<UserRecordingShareRespVO> generateBookRecordingShare(
            @Parameter(description = "绘本ID", required = true, example = "1") @PathVariable("bookId") Long bookId) {
        Long userId = getLoginUserId();
        log.info("生成绘本配音分享信息，用户ID: {}, 绘本ID: {}", userId, bookId);

        UserRecordingShareRespVO result = recordingsService.generateBookRecordingShare(userId, bookId);
        return success(result);
    }

    @PostMapping("")
    @Operation(summary = "保存用户录制音频")
    @PreAuthenticated
    public CommonResult<Boolean> saveUserRecording(@RequestBody @Valid UserRecordingCreateReqVO reqVO) {
        Long userId = getLoginUserId();
        log.info("保存用户录制音频，用户ID: {}, 请求参数: {}", userId, reqVO);
        
        recordingsService.saveUserRecording(userId, reqVO);
        return success(true);
    }

    @GetMapping("/page/{pageId}")
    @Operation(summary = "获取用户录制音频")
    @PreAuthenticated
    public CommonResult<UserRecordingDetailRespVO> getUserRecordingByPage(
            @Parameter(description = "页面ID", required = true, example = "10") @PathVariable("pageId") Long pageId) {
        Long userId = getLoginUserId();
        log.info("获取用户录制音频，用户ID: {}, 页面ID: {}", userId, pageId);
        
        UserRecordingDetailRespVO result = recordingsService.getUserRecordingByPage(userId, pageId);
        return success(result);
    }

    @DeleteMapping("/{recordingId}")
    @Operation(summary = "删除配音记录")
    @PreAuthenticated
    public CommonResult<Boolean> deleteRecording(
            @Parameter(description = "配音记录ID", required = true, example = "1") @PathVariable("recordingId") Long recordingId) {
        Long userId = getLoginUserId();
        log.info("删除配音记录，用户ID: {}, 配音记录ID: {}", userId, recordingId);

        recordingsService.deleteRecording(userId, recordingId);
        return success(true);
    }

    @DeleteMapping("/book/{bookId}")
    @Operation(summary = "删除绘本的所有配音记录")
    @PreAuthenticated
    public CommonResult<Boolean> deleteBookRecordings(
            @Parameter(description = "绘本ID", required = true, example = "10") @PathVariable("bookId") Long bookId) {
        Long userId = getLoginUserId();
        log.info("删除绘本配音记录，用户ID: {}, 绘本ID: {}", userId, bookId);

        recordingsService.deleteBookRecordings(userId, bookId);
        return success(true);
    }

    @PostMapping("/{recordingId}/share")
    @Operation(summary = "分享配音记录")
    @PreAuthenticated
    public CommonResult<Boolean> shareRecording(
            @Parameter(description = "配音记录ID", required = true, example = "1") @PathVariable("recordingId") Long recordingId) {
        Long userId = getLoginUserId();
        log.info("分享配音记录，用户ID: {}, 配音记录ID: {}", userId, recordingId);
        
        recordingsService.shareRecording(userId, recordingId);
        return success(true);
    }

    @GetMapping("/book-pages/{pageId}/audio-status")
    @Operation(summary = "检查页面音频状态")
    @PreAuthenticated
    public CommonResult<PageAudioStatusRespVO> getPageAudioStatus(
            @Parameter(description = "页面ID", required = true, example = "1") @PathVariable("pageId") Long pageId) {
        Long userId = getLoginUserId();
        log.info("检查页面音频状态，用户ID: {}, 页面ID: {}", userId, pageId);
        
        PageAudioStatusRespVO result = recordingsService.getPageAudioStatus(userId, pageId);
        return success(result);
    }

}
